import 'package:flutter/material.dart';

class KeepAliveWarpper extends StatefulWidget {
  final Widget? child;
  final bool isKeepAlive;
  const KeepAliveWarpper({ Key? key,required this.child,required this.isKeepAlive  }) : super(key: key);

  @override
  _KeepAliveWarpperState createState() => _KeepAliveWarpperState();
}

class _KeepAliveWarpperState extends State<KeepAliveWarpper> with AutomaticKeepAliveClientMixin{
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return widget.child!;
  }

  @override
  bool get wantKeepAlive => widget.isKeepAlive;

  @override
  void didUpdateWidget(covariant KeepAliveWarpper oldWidget) {
    //oldWidget是旧的组件，widget是当前新的组件
    if(oldWidget.isKeepAlive!= widget.isKeepAlive){
      updateKeepAlive();
    }
    super.didUpdateWidget(oldWidget);
    
  }

}